﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using Photon;

public class MotionController : Photon.MonoBehaviour 
{	
	[HideInInspector] public NavMeshAgent agent;
	[HideInInspector] public bool following = false;
	[HideInInspector] public Collider followTarget;
	public float walkSpeed = 10;
	public float runSpeed = 12;
	public float defaultMinDist = 1;
	[HideInInspector] public float minDistance = 1;
	[HideInInspector]public Vector3 destination;

	[HideInInspector] public Attacker attacker;
	[HideInInspector] public Vector3 closestPointToFollowTarget;
	[HideInInspector] public PhotonView photView;
	[HideInInspector] public LifeController lifeController;
	[HideInInspector] public float speed;
	
	[HideInInspector]public int followTargetID = 0;
	[HideInInspector]public LowResPositionSync lrps;
	 public float attackDistanceBuffer = 0.5F;
	 public virtual void Awake()
	 {
		photView = PhotonView.Get(this);
		agent = this.GetComponent<UnityEngine.AI.NavMeshAgent>();
		attacker = this.GetComponent<Attacker> ();
		lifeController = this.GetComponent<LifeController>();
		lrps = this.GetComponent<LowResPositionSync>();
		speed = walkSpeed;
	 }
	 public virtual void OnJoinedRoom()
	 {	
		 Debug.Log("in a room");
		if (photView.isMine || (photView.isSceneView && PhotonNetwork.isMasterClient))
		{	
			Debug.Log("we own this  photon  view");
			GoTo(transform.position);
		}
	 }

	public virtual void GoTo(Vector3 position)
	{
		
	}

	public virtual void Follow(Collider target)
	{
		
	}
	public virtual void Attack(LifeController life)
	{
		
	}

	public virtual void PickUp(GameObject obj)
	{
		
	}

	public virtual void LookAt(Vector3 targetPosition)
	{

	}
	public virtual void OnEnable()
	{
		if (agent != null)
		{
			agent.enabled = true;
		}
		destination = transform.position;
	}
	public virtual void OnDisable()
	{
		if (agent != null)
		{
			agent.enabled = false;
		}
	}

	public virtual void Update () 
	{
		CarryOutState();
	}
	public float DistanceToFollowTarget()
	{
		closestPointToFollowTarget = followTarget.ClosestPoint(this.transform.position);
		return GameFunctions.PlanarDistanceBetween (this.transform.position, closestPointToFollowTarget);
	}
	public void CarryOutState()
	{	
		if (following)
		{	
			if (followTarget == null)
			{
				GoTo(transform.position);
			} else
			{	
				
				if (DistanceToFollowTarget() > minDistance)
				{
					agent.destination = closestPointToFollowTarget;
				} else
				{	
					agent.destination = this.transform.position;
				}
			}
		} else
		{
			if(agent != null)
			{	
				if (destination != agent.destination)
				{
					agent.destination = destination;
				}
				if (agent.speed != speed)
				{
				agent.speed = speed;
				}
			}
		}
	}
	public virtual void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
	{
		if (stream.isWriting)
		{
			stream.SendNext(destination);
			stream.SendNext(followTargetID);
			stream.SendNext(minDistance);
			stream.SendNext(following);
			stream.SendNext(speed);
		}
		if (stream.isReading)
		{
			Vector3 newDestination = (Vector3)stream.ReceiveNext();
			if (newDestination != destination) destination = newDestination;
			int newFollowTargetID = (int)stream.ReceiveNext();
			minDistance = (float)stream.ReceiveNext();
			following = (bool)stream.ReceiveNext();
			if (newFollowTargetID != followTargetID)
			{
				FollowID(newFollowTargetID);
			}
			speed = (float)stream.ReceiveNext();
		}
	}
	public virtual void FollowID(int viewID)
	{	
		followTargetID = viewID;
		PhotonView pv =  PhotonView.Find(viewID);
		if (pv== null)
		{
		} else
		{
			Debug.Log("following photon view with ID "+ viewID.ToString());
		LifeController lc = PhotonView.Find(viewID).GetComponent<LifeController>();
			if (lc.col == null)
			{
				Debug.Log("no lifecontroller was found");
			}else
			{
				followTarget = lc.col;
				Debug.Log(followTarget.transform.name);
			}
		}
	}
	public virtual void StopAttacking()
	{
		GoTo(transform.position);
	}
}
